package com.neuedu.web.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述
 *
 *
 * 构造的servlet 路径
 *   http://localhost:8080/web/hello
 *
 *
 *
 * @data 2024/11/1211:59
 */
// @WebServlet(urlPatterns = "/hello",loadOnStartup = 10,initParams = {
//         @WebInitParam(name = "username",value = "root"),
//         @WebInitParam(name = "password",value = "123456"),
// })
public class HelloWorldServlet  extends HttpServlet {

    public HelloWorldServlet() {
        System.out.println("HelloWorldServlet.HelloWorldServlet");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("HelloWorldServlet.init");

        String username = getInitParameter("username");
        String password = getInitParameter("password");

        System.out.println("username = " + username);
        System.out.println("password = " + password);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取服务器时间
        System.out.println("控制台输出：访问Servlet");

        String time = new Date().toString();


        //通过 resp  给浏览器响应内容
        PrintWriter writer = resp.getWriter();

        writer.write("server Time :"+time);
        writer.flush();
        writer.close();
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
